﻿#region Copyright(C) 2018 FI Ltd. All rights reserved.
// ==================================================================================================
// Copyright(C) 2018 FI Ltd. All rights reserved.
//
// SYSTEM NAME	    : FI.Net Application Framework
// COMPONENT ID	    : FI.Core
// COMPONENT DESC   : FI.Net Core Foundation
//
// CREATED DATE/BY  : 18 Aug 2018 / Tian Mi
//
// REVISION HISTORY :	
// DATE/BY			SR#/CS/PM#/OTHERS		DESCRIPTION OF CHANGE
// ==================================================================================================
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FI.Core.IoC;
using FI.Core.Configuration;
using FI.Core.Caching.Configuration;

namespace FI.Core.Caching
{
    ///<summary>
    /// The cache store factory to create <see cref="HttpRuntimeCacheStoreFactory"/>.
    /// </summary>
    [MapTo(typeof(ICacheStoreFactory), Lifetime = Lifetime.Singleton)]
    [ConfigurationElement(typeof(HttpRuntimeCacheStoreFactoryData))]
    public class HttpRuntimeCacheStoreFactory : ICacheStoreFactory
    {
        /// <summary>
        /// Gets the cache store.
        /// </summary>
        /// <param name="keyPrefix">The key prefix.</param>
        /// <param name="priority">The priority.</param>
        /// <param name="expirationMode">The expiration mode.</param>
        /// <param name="expirationTime">The expiration time.</param>
        /// <param name="timeFormat">The time format.</param>
        /// <param name="dependentFilePath">The depedent file path.</param>
        /// <returns>
        /// The cache store.
        /// </returns>
        public ICacheStore GetCacheStore(string keyPrefix, CacheItemPriority priority, CacheItemExpirationMode expirationMode, TimeSpan? expirationTime, string timeFormat, string dependentFilePath)
        {
            string expiredTimeOrTimeFormatOrFileName = null;
            if (expirationMode == CacheItemExpirationMode.TimeFormat)
            {
                expiredTimeOrTimeFormatOrFileName = timeFormat;
            }
            if (expirationMode == CacheItemExpirationMode.FileDependency)
            {
                expiredTimeOrTimeFormatOrFileName = dependentFilePath;
            }
            if (string.IsNullOrEmpty(expiredTimeOrTimeFormatOrFileName) && expirationTime.HasValue)
            {
                expiredTimeOrTimeFormatOrFileName =  expirationTime.Value.ToString();
            }
            return new HttpRuntimeCacheStore(keyPrefix, priority, expirationMode, expiredTimeOrTimeFormatOrFileName);
        }
    }
}
